package com.micklebes.converter;

import com.google.appengine.api.datastore.KeyFactory;
import com.micklebes.entity.Product;
import com.micklebes.entity.ProductBO;

/**
 * User: th3sly
 * Date: 1/15/12
 * Time: 9:33 PM
 */
public class ProductConverter {
    public static ProductBO convertToBO(Product product) {
        ProductBO productBO = new ProductBO();
        productBO.setAmount(product.getAmount());
        productBO.setName(product.getName());
        productBO.setPrice(product.getPrice());

        if (product.getKey() != null) {
            productBO.setKey(KeyFactory.keyToString(product.getKey()));
        }

        if (product.getStorageKey() != null) {
            productBO.setStorageKey(KeyFactory.keyToString(product.getStorageKey()));
        }

        return productBO;
    }

    public static Product convertToDAOEntity(ProductBO productBO) {
        Product product = new Product();
        product.setAmount(productBO.getAmount());
        product.setName(productBO.getName());
        product.setPrice(productBO.getPrice());

        if (productBO.getKey() != null) {
            product.setKey(KeyFactory.stringToKey(productBO.getKey()));
        }

        if (productBO.getStorageKey() != null) {
            product.setStorageKey(KeyFactory.stringToKey(productBO.getStorageKey()));
        }

        return product;
    }
}
